/*
Source : https://leetcode.com/problems/search-for-a-range/
Author : nflush@outlook.com
Date   : 2016-08-23
*/

/*
34. Search for a Range

    Total Accepted: 95728
    Total Submissions: 319282
    Difficulty: Medium

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm's runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

Subscribe to see which companies asked this question
*/

class Solution { // 12ms
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> ret(2, -1);
        int min = 0;
        int max = nums.size() -1;
        int mid;
        int left = -2, right = max;
        while (min <= max){
            mid = (max + min) / 2;
            if (nums[mid] > target){
                max = mid -1;
            } else if (nums[mid] < target){
                min = mid +1;
            } else {
                left = mid;
                right = mid;
                break;
            }
        }
        while (min < left){
            mid = (left + min) / 2;
            if (nums[mid] >= target){
                left = mid-1;
            } else if (nums[mid] < target){
                min = mid +1;
            }
        }
        while (right < max){
            mid = (max + right) / 2;
            if (nums[mid] > target){
                max = mid - 1;
            } else if (nums[mid] <= target){
                right = mid+1;
            }
        }
        if (left != -2){
            ret[0] = (left>=0 && nums[left] == target) ? left : left + 1;
            ret[1] = nums[right] == target ? right : right-1;
        }
        return std::move(ret);
    }
};

class Solution { // 12ms
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> ret(2, -1);
        int min = 0;
        int max = nums.size() -1;
        int mid = (max + min) / 2;
        int left = target - 1, right = target + 1;
        while (min < max){
            mid = (max + min) / 2;
            if (nums[mid] > left){
                max = mid -1;
            } else if (nums[mid] < left){
                min = mid +1;
            } else {
                min = mid +1;
                if (nums[mid+1])
            }
        }
        while (min < left){
            mid = (left + min) / 2;
            if (nums[mid] >= target){
                left = mid-1;
            } else if (nums[mid] < target){
                min = mid +1;
            }
        }
        while (right < max){
            mid = (max + right) / 2;
            if (nums[mid] > target){
                max = mid - 1;
            } else if (nums[mid] <= target){
                right = mid+1;
            }
        }
        if (left != -2){
            ret[0] = (left>=0 && nums[left] == target) ? left : left + 1;
            ret[1] = nums[right] == target ? right : right-1;
        }
        return std::move(ret);
    }
};